"ui";

ui.layout(
  <frame>
    <vertical>
      <appbar>
        <toolbar bg="#6200ee" gravity="center" title="抖音助手"></toolbar>
      </appbar>

      <horizontal marginTop="10" gravity="center">
        <button
          w="auto"
          text="+新增步骤"
          bg="#6200ee"
          color="#ffffff"
          id="creatStep"
        />
        <button
          w="auto"
          id="startTask"
          gravity="center"
          text="开始任务"
          margin="10"
          padding="10"
          bg="#6200ee"
          color="#ffffff"
        />
      </horizontal>
      <card
        w="*"
        h="auto"
        margin="10"
        
        cardCornerRadius="10dp"
        cardElevation="3dp"
      >
        <vertical padding="20 0" gravity="center">
          <horizontal >
            <horizontal>
              <TextView w="auto" text="打开app" />
              <input w="*" id="app" minWidth="150" text="抖音" />
            </horizontal>
            <horizontal>
              <TextView w="auto" text="执次数" />
              <input
                w="*"
                id="num"
                minWidth="150"
                text="1"
                inputType="number"
              />
            </horizontal>
          </horizontal>
          <horizontal>
            
            <TextView w="auto" text="定时执行" />
            <input w="auto" id="delay" minWidth="150" text="0"  inputType="number" />
            <TextView w="auto" text="分钟" />
          </horizontal>
        </vertical>
      </card>
      <card w="*" h="*" margin="10" cardCornerRadius="10dp" cardElevation="3dp">
        <ScrollView>
          <vertical padding="20">
            <vertical w="*" gravity="center">
              <TextView w="auto" text="添加操作步骤" />
            </vertical>
            <linear orientation="vertical" id="container"></linear>
          </vertical>
        </ScrollView>
      </card>
    </vertical>
  </frame>
);
ui.index = 0;
ui.typeList = ["下一个", "上一个", "点赞", "关注", "分享", "评论","搜索"];
ui.controlList = [];
ui.statusBarColor("#6200ee");
ui.startTask.click((e) => {
  const input = ui.app.text();
  const num = ui.num.text();
  const delay = ui.delay.text()
  const obj = {
    app: input,
    steps: ui.controlList,
    num: num - 0,
    delay: delay-0
  };
  const init = require("./control");
  const events = require("../../common/events");
  threads.shutDownAll()
  
  const thread = threads.start(() => {
    if(obj.delay) {
      log('执行延时'+ obj.delay+'分')
       sleep(obj.delay * 60 * 1000 )
    }
    init(obj);
  });
  events.remove("result");
  events.on("result", function () {
    thread.interrupt();
    log("清除线程");
  });
});
ui.creatStep.click(async () => {
  const selectIndex = await dialogs.singleChoice(
    "选择操作类型",
    ui.typeList,
    0
  );
  const selectVal = ui.typeList[selectIndex];
  ui.index++;
  const textView = ui.inflate(
    <horizontal marginTop="10">
      <TextView w="auto" text="步骤{{ui.index}}:" color="#666666" />
      <input w="*" marginLeft="20" text="{{selectVal}}" inputType="none" />
    </horizontal>,
    ui.container
  );
  ui.container.addView(textView);
  ui.controlList.push({ val: selectVal, step: ui.index });
});
